package com.qf.controller;
import com.qf.result.Result;
import com.qf.service.GoodsService;
import com.qf.vo.GoodsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/08/16:33
 * @Description:
 */
@Controller
@RequestMapping("/goodsTypeController")
public class goodsTypeController {
  @Autowired
    private GoodsService goodsService;
  @RequestMapping("/getGoodsTypeList")
  public String getGoodsTypeList(ModelMap modelMap){
      List<GoodsType> goodsTypeList =goodsService.getGoodsTypeList();
      modelMap.put("goodsTypeList",goodsTypeList);
      return "goods/goodsTypeList";
  }
    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page,Integer id,ModelMap modelMap) {
        // 此时page就是addUser
        modelMap.put("id",id);
        return "goods/" + page; //
    }
     @RequestMapping("getTypePid/{pid}")
     @ResponseBody
    public List<GoodsType> getGoodsTypebyPid(@PathVariable(value = "pid") Integer pid){
         List<GoodsType> typeList = goodsService.getGoodsTypeByPid(pid);
         return  typeList;
    }

    @RequestMapping("/getGoodsTypeTreeList")
    @ResponseBody
    public Result getGoodsTypeTreeList(){
        List<GoodsType> goodsTypeList =goodsService.getGoodsTypeList();
        List<Map<String,Object>> list = new ArrayList<>();
        for (GoodsType goodsType : goodsTypeList) {
            Map<String,Object> map =new HashMap<>();
            map.put("id",goodsType.getId());
            map.put("pid",goodsType.getGoodsTypePid());
            map.put("name",goodsType.getGoodsTypeName());
            list.add(map);
        }
        return Result.success(list);
    }

    @RequestMapping("/delNodes/{id}")
    @ResponseBody
    public Result delNodes(@PathVariable(value = "id") Integer id){
         return Result.write( goodsService.deleteNode(id));
    }
    @RequestMapping("/updateNodes")
    @ResponseBody
    public  Result updateNode(GoodsType goodsType){
   return   Result.write( goodsService.updateNode(goodsType));
    }
    @RequestMapping("/addNode")
    @ResponseBody
    public Result addNode(GoodsType goodsType){
     return Result.write(goodsService.addGoodsType(goodsType));
    }
    @RequestMapping("/moveNodes/{trId}/{tarId}")
    @ResponseBody
    public Result moveNodes(@PathVariable(value = "trId") Integer trId,@PathVariable(value = "tarId") Integer tarId){
      return Result.write(goodsService.moveNodes(trId,tarId));
    }
}
